
package com.zcp.notepad;

import com.zcp.notepad.NotePad.Notes;

import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Environment;
import android.text.format.DateUtils;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

public class ExportActivity extends SelectList {
    /**
     * 处理done按钮点击事件
     */
    @Override
    protected void dealDoneOnClick() {
        ArrayList<HashMap<String, Object>> checkList = getCheckObject();
        if (checkList.size() <= 0) {
            return;
        }
        boolean shouldCheckPassword = false;
        int lock = NotePad.DataValue.IS_UNLOCK;

        StringBuffer buffer = new StringBuffer(PROJECTION[0] + " in(");
        for (HashMap<String, Object> element : checkList) {
            buffer.append(element.get(PROJECTION[0]) + ",");

            lock = Integer.valueOf(element.get(NotePad.Notes.LOCK).toString());
            if (NotePad.DataValue.IS_LOCK == lock) {
                shouldCheckPassword = true;
            }
        }
        buffer.delete(buffer.length() - 1, buffer.length());
        buffer.append(")");

        if (shouldCheckPassword) {
            Util.getCheckPasswordDialog(this, getString(R.string.Export_txt_file), null,
                    myInterfaceOkCancelClick, buffer.toString()).show();
        } else {
            exportFile(buffer.toString());
        }

    }

    @Override
    protected void dealCancelOnClick() {
        finish();

    }

    private void exportFile(String whereCondition) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Cursor cursor = getContentResolver().query(getIntent().getData(), new String[] {
                    Notes.NOTE, Notes.CREATED_DATE
            }, whereCondition, null, Notes.DEFAULT_SORT_ORDER);

            StringBuffer buf = new StringBuffer();
           
            if (cursor != null && cursor.moveToFirst()) {
                do {

                    buf.append("--"
                            + DateUtils.formatDateTime(this, cursor.getLong(1),
                                    DateUtils.FORMAT_ABBREV_ALL) + "\n");
                    buf.append("--" + cursor.getString(0) + "\n");

                } while (cursor.moveToNext());
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());

            String title = "myNotes_" + calendar.get(Calendar.YEAR)
                    + (calendar.get(Calendar.MONTH) + 1) + calendar.get(Calendar.DAY_OF_MONTH);

            boolean success = Util.createFile(title, buf.toString());

            if (success) {
                String show = this.getString(R.string.sdcard_export);
                show = String.format(show, title);
                Toast.makeText(this, show, Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, this.getString(R.string.sdcard_export_error),
                        Toast.LENGTH_SHORT).show();
            }

        } else {
            Toast.makeText(this, this.getString(R.string.sdcard_cannot_user), Toast.LENGTH_SHORT)
                    .show();
        }

        finish();
    }

    class MyInterfaceOkCancelClick implements InterfaceOkCancelClick {

        @Override
        public void dealWithOk(boolean isCheckOk, boolean valid, DialogInterface dialog,
                TextView passwordTv, Object tagObject) {
            // 密码无效情况下。
            if (!valid) {
                Util.setDialogCanDismiss(dialog, false);
                dialog.dismiss();
                passwordTv.setText("");
                showText(R.string.invalid_password);
                return;
            }

            if (isCheckOk) {
                Util.setDialogCanDismiss(dialog, true);
                dialog.dismiss();
                exportFile(tagObject.toString());
            } else {
                Util.setDialogCanDismiss(dialog, false);
                dialog.dismiss();
                passwordTv.setText("");
                showText(R.string.wrong_password);
            }
        }

        @Override
        public void dealWithCancel(DialogInterface dialog, Object tagObject) {
            Util.setDialogCanDismiss(dialog, true);
            dialog.dismiss();
        }
    }

    private void showText(int StringId) {
        Toast.makeText(this, getString(StringId), Toast.LENGTH_SHORT).show();
    }

    private MyInterfaceOkCancelClick myInterfaceOkCancelClick = new MyInterfaceOkCancelClick();
}
